<template>
  <a-modal
    :open="open"
    width="700px"
    :title="rowData?.id ? '修改国家医保诊断字典' : '新增国家医保诊断字典'"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="诊断代码" class="w-1/2" v-bind="validateInfos.icd10Code">
        <a-input v-model:value="formState.icd10Code" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="附加码" class="w-1/2">
        <a-input v-model:value="formState.icd10AddCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="诊断名称" class="w-1/2" v-bind="validateInfos.icd10Name">
        <a-input v-model:value="formState.icd10Name" placeholder="请输入" @blur="onChangeDiseTypeName" />
      </a-form-item>
      <a-form-item label="助记码" class="w-1/2">
        <a-input v-model:value="formState.zjm" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { debounce } from 'lodash-es'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({
  icd10Code: '',
  icd10Name: ''
})
const rulesState = ref({
  icd10Code: [{ required: true, message: '请输入诊断代码' }],
  icd10Name: [{ required: true, message: '请输入诊断名称' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)
const onChangeDiseTypeName = debounce(async () => {
  const { data, code } = await post('/common/util/getPinyin', { name: formState.value.icd10Name })
  code === 0 && (formState.value.zjm = data)
}, 500)
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  const { code, msg } = await post('/common/masterData/doSaveNmiDictIcd10', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('ok')
}

watch(
  () => props.open,
  () => {
    formState.value = { ...props.rowData }
  }
)
</script>
<style lang="less" scoped></style>
